package com.dartsstats;

import java.util.List;

import com.dartsstats.R;
import com.dartsstats.R.id;
import com.dartsstats.R.layout;
import com.dartsstats.R.menu;
import com.dartsstats.datasource.TeamDataSource;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.support.v4.app.NavUtils;

public class SetMatchSettingsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_match_settings);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		addTeamsToSpinner(R.id.homeTeam);
		addTeamsToSpinner(R.id.awayTeam);
	}
	
	private void addTeamsToSpinner(int spinnerId) {
		Spinner spinner = (Spinner) findViewById(spinnerId);
		List<Team> teamList = new TeamDataSource(this).getAllTeams();
		ArrayAdapter<Team> adapter = new ArrayAdapter<Team>(this,
				android.R.layout.simple_spinner_item, teamList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_set_match_settings, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void setLineups(View view) {
		Intent intent = new Intent(this, SetLineupsActivity.class);
		Spinner homeTeamSpinner = (Spinner) findViewById(R.id.homeTeam);
		Spinner awayTeamSpinner = (Spinner) findViewById(R.id.awayTeam);
		Team homeTeam = (Team)homeTeamSpinner.getSelectedItem();
		Team awayTeam = (Team)awayTeamSpinner.getSelectedItem();
		
		intent.putExtra("HOMETEAM_ID", homeTeam.getId()); 
		intent.putExtra("AWAYTEAM_ID", awayTeam.getId()); 
		startActivity(intent);
	}

}
